// pages/submitOrder/submitOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:'/static/images/no-pic.png',
    id:0,
    serverUrl:'',
    serverHeader:{},
    info:{address:{id:0},items:{}},
    sub:{id:0,address:0,num:1,day_tip:'',day:'',time:'',note:'',city:'',imgs:[]},
    insufficient: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id,
      'sub.id':options.id,
      serverUrl:wx.util.interfaceUrl()+"upload",
      serverHeader:{
        'Deer-Device-Type':'wxapp',
        'Deer-Api-Version':wx.util.ver,
        'Deer-Api-Token': wx.util.getToken()
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(wx.util.fwDate.length==3){
      this.setData({
        'sub.day_tip':wx.util.fwDate[0],
        'sub.day':wx.util.fwDate[1],
        'sub.time':wx.util.fwDate[2],
      })
    }
    this.load();
  },
  async load(){
    wx.util.fwDate=[];
    let res=await wx.util.post("card_order/add_info",{id:this.data.id});
    this.setData({
      info: res.data,
      price: res.data.price,
      'sub.address': res.data.address.id,
    })
  },
  result(e) {
    this.setData({
      'sub.imgs': e.detail.imgArr
    })
  },
  remove(e) {
    this.data.sub.imgs.splice(e.detail.index,1);
    this.setData({
      'sub.imgs': this.data.sub.imgs
    })
  },
  zxTime(){
    wx.navigateTo({
      url: "/pages/date/date?gg_id="+this.data.info.deerhome_items_gg_hash
    })
  },
  change(e){
    if(e.detail.value>this.data.info.num_left){
      e.detail.value=this.data.info.num_left;
      wx.util.toast("当前仅剩余"+this.data.info.num_left+"次");
    }
    this.setData({
      'sub.num': e.detail.value
    })
  },
  chooseAddr() {
    wx.navigateTo({
      url: "/pages/address/address"
    })
  },
  async btnPay() {
    if(this.data.sub.address==0){
      wx.util.toast("请选择地址");
      return;
    }
    if(this.data.sub.day==''){
      wx.util.toast("请选择服务时间");
      return;
    }
    let city=wx.getStorageSync("city");
    if(city){
      this.setData({
        'sub.city': city
      })
    }

    let order=await wx.util.post("card_order/creat_service",this.data.sub,false);
    wx.setStorageSync("order_tab","-1");
    wx.switchTab({
      url:"/pages/myOrder/myOrder"
    })
  },

})